package com.zhongwang.business.module.basic.controller;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.common.ValidResult;
import com.zhongwang.business.module.basic.model.OrgModel;
import com.zhongwang.business.module.basic.service.OrgBasicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class OrgController extends BaseController {

    @Autowired
    private OrgBasicService orgBasicService;

    /**
     * 首页跳转至审核组织架构页面
     *
     * @param model
     * @return
     */
    @GetMapping("/basic/org")
    @RequiresPermissions({"basic:org:view"})
    public String orgPage(Model model) {
        model.addAttribute("posts", orgBasicService.findDictByGroupName("审核级别"));
        return "/basicManage/org/page";
    }


    /**
     * 获取列表
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/orgList")
    public List<OrgModel> selectAllList(String name) {
        return orgBasicService.selectList(name);
    }

    /**
     * 部门名称唯一验证
     */
    @ResponseBody
    @PostMapping("/orgCheckUnique")
    public ValidResult checkUnique(OrgModel org) {
        OrgModel m = orgBasicService.findOrgByName(org.getName());
        if (org.getId() == null) {
            return ValidResult.result(m);
        }
        if (m == null || org.getId().equals(m.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 新增
     * @param org
     * @return
     */
    @ResponseBody
    @PostMapping("/orgSave")
    public JsonResult saveOrg(OrgModel org) {
        return isNotNull(orgBasicService.saveOrg(org));
    }

    /**
     * 编辑
     * @param org
     * @return
     */
    @ResponseBody
    @PostMapping("/orgUpdate")
    public JsonResult updateOrg(OrgModel org) {
        return isNotNull(orgBasicService.updateOrg(org));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/orgDel")
    public JsonResult del(Integer id) {
        return jsonResult(orgBasicService.delOrg(id));
    }

}